package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.linkbeans.SysContractLesson;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.linkservices.SysContractLessonService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/")
@RestController
public class SysContractLessionController {

    @Autowired
    private SysContractLessonService sysContractLessonService;

    //使用get需要添加默认get函数
    @GetMapping(value = "/contractlessions")
    @RequiresAuthentication
    public ModelMap getContractLessonList(){
        List<SysContractLesson> sysContractLessonList = sysContractLessonService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_CONTRACT_LESSON_SUCCEED, StaticConstant.CONTRACT_LESSON_LIST, sysContractLessonList);
    }

    @GetMapping(value = "/contractlession/{cid}/{lsnid}")
    @RequiresAuthentication
    public ModelMap getContractLessonByUK(@PathVariable Integer cid,@PathVariable Integer lsnid){
        SysContractLesson sysContractLesson = sysContractLessonService.getContractLesson(cid,lsnid);
        return MsgUtil.retMsg(SysMessage.GET_CONTRACT_LESSON_SUCCEED, StaticConstant.CONTRACT, sysContractLesson);
    }


    /*
     *
     * http://localhost:8080/api/contractlession?user_id=1&role_id=1
     * */
    @PostMapping(value = "/contractlession")
    @RequiresAuthentication
    public ModelMap addContractLesson(@RequestHeader("Authorization") String token,
                                      @RequestParam("contract_id") Integer cid,
                                      @RequestParam("lesson_id") Integer lsnid,
                                      @RequestParam("points")  Integer points){

        try{
            SysContractLesson sysContractLesson = new SysContractLesson(cid, lsnid,points);
            sysContractLesson.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysContractLessonService.add(sysContractLesson);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CONTRACT_LESSON_SUCCEED, null ,null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CONTRACT_LESSON_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    /*
     *
     * http://localhost:8080/api/contractlession?user_id=1&role_id=1
     * */
    @PutMapping(value = "/contractlession")
    @RequiresAuthentication
    public ModelMap updateContractLesson(@RequestParam("contract_id") Integer cid,
                                         @RequestParam("lesson_id") Integer lsnid,
                                         @RequestParam("points")  Integer points){

        SysContractLesson sysContractLesson = sysContractLessonService.getContractLesson(cid, lsnid);
        sysContractLesson.setPoints(points);
        if(sysContractLesson != null){
            try{
                Integer r = sysContractLessonService.update(sysContractLesson);
                if(r > 0){
                    return MsgUtil.retMsg(SysMessage.UPDATE_CONTRACT_LESSON_SUCCEED, null ,null);
                }else{
                    return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
                }
            }catch (Exception e){
                return ErrorUtil.retErrMsg(e);
            }
        }else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }
}
